package per.hx.bs.web.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.entity.sys.SysJobDO;
import per.hx.bs.entity.sys.SysUserDO;
import per.hx.bs.util.PageUtils;
import per.hx.bs.util.Query;
import per.hx.bs.web.sys.service.SysJobService;

import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/4/29  0:37
 */
@Controller
@RequestMapping(value = "/sys/job")
public class JobController extends BaseController {
    private final String prefix = "system/job";

    private final
    SysJobService jobService;

    @Autowired
    public JobController(SysJobService jobService) {
        this.jobService = jobService;
    }

    @BsCLog("进入岗位管理页面")
    @RequiresPermissions("sys:job:job")
    @GetMapping
    public String job() {
        return prefix + "/job";
    }

    @BsCLog("获取岗位管理数据")
    @RequiresPermissions("sys:job:job")
    @GetMapping(value = "/pageList")
    @ResponseBody
    public PageUtils job(@RequestParam Map<String, Object> map) {
        Query query = new Query(map);
        List<SysJobDO> jobDOS = jobService.listJob(query);
        Integer count = jobService.countJob(map);
        return new PageUtils(jobDOS, count);
    }

    @BsCLog("进入岗位管理添加页面")
    @RequiresPermissions("sys:job:add")
    @GetMapping(value = "/add")
    public String jobAdd() {
        return prefix + "/add";
    }

    @BsCLog("添加岗位")
    @RequiresPermissions("sys:job:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public ReturnDTO jobAdd(SysJobDO jobDO) {
        jobDO.setCreateId(getUserId());
        return ReturnDTO.check(jobService.insert(jobDO));
    }

    @BsCLog("进入岗位管理修改页面")
    @RequiresPermissions("sys:job:edit")
    @GetMapping(value = "/edit/{id}")
    public String jobEdit(@PathVariable("id") Long id, Model model) {
        SysJobDO jobDO = jobService.selectOne(id);
        model.addAttribute("jobDO", jobDO);
        return prefix + "/edit";
    }

    @BsCLog("修改岗位")
    @RequiresPermissions("sys:job:edit")
    @PostMapping(value = "/edit")
    @ResponseBody
    public ReturnDTO jobEdit(SysJobDO jobDO) {
        jobDO.setUpdateId(getUserId());
        return ReturnDTO.check(jobService.update(jobDO));
    }

    @BsCLog("移除岗位")
    @RequiresPermissions("sys:job:remove")
    @PostMapping(value = "/remove")
    @ResponseBody
    public ReturnDTO jobRemove(@RequestParam("ids[]") Long[] ids) {
        return ReturnDTO.check(jobService.remove(ids));
    }

    @BsCLog("进入岗位管理授予页面")
    @RequiresPermissions("sys:job:user")
    @GetMapping(value = "/{id}/user")
    public String jobUser(@PathVariable("id") Long id, Model model) {
        SysJobDO jobDO = jobService.selectOne(id);
        model.addAttribute("job", jobDO);
        return prefix + "/jobUser";
    }

    @BsCLog("岗位管理授予")
    @RequiresPermissions("sys:job:user")
    @PostMapping(value = "/user")
    @ResponseBody
    public ReturnDTO jobUser(SysJobDO jobDO) {
        jobDO.setCreateId(getUserId());
        return jobService.insertUserJob(jobDO);
    }

    @BsCLog("查看人员列表")
    @GetMapping(value = "/{id}/userList")
    public String jobUserList(@PathVariable("id") Long id, Model model) {
        model.addAttribute("jobId", id);
        return prefix + "/userList";
    }

    @BsCLog("获取人员列表")
    @GetMapping(value = "/userList")
    @ResponseBody
    public PageUtils jobUserList(@RequestParam Map<String, Object> map) {
        Query query = new Query(map);
        List<SysUserDO> userDOS = jobService.selectUserList(query);
        System.out.println(userDOS);
        Integer count = jobService.countUserList(map);
        return new PageUtils(userDOS, count);
    }

    @BsCLog("将用户移除岗位")
    @RequiresPermissions("sys:job:user")
    @GetMapping(value = "/remove/{userId}")
    @ResponseBody
    public ReturnDTO removeUser(@PathVariable("userId") Long userId) {
        return ReturnDTO.check(jobService.removeUser(userId));
    }

}
